<?php

define('ACC', true);

require('../includes/init.php');
$goodsType = new GoodsTypeModel();

if($_GET['act'] == 'list'){
	
	require('../library/Fenye.class.php');
	$fenye = new Fenye();
	
	$rowCount = $goodsType->rowCount();
	$pageSize = 10;
	
	$fenye->setMessage($rowCount, $pageSize);
	$pageCount = $fenye->getPageCount();
	
	if($pageCount <= 5){
		$fenye->setMaxPageSize($pageCount);
	}else{
		$fenye->setMaxPageSize(5);
	}
	
	if(isset($_GET['pageNow'])){
		$pageNow = $_GET['pageNow'] + 0;
		if($pageNow <= 0){
			$pageNow = 1;
		}else if($pageNow > $pageCount){
			$pageNow = $pageCount;
		}
	}else{
		$pageNow = 1;
	}
	
	$fenye->setPageNow($pageNow);
	
	$url = 'goods_type.php?act=list';
	$navigate = $fenye->getFenyeCode($url);
	
	$smarty->assign('pageNow', $pageNow);
	$smarty->assign('firstPage', 1);
	$smarty->assign('lastPage', $pageCount);
	$smarty->assign('pageCount', $pageCount);
	$smarty->assign('navigate', $navigate);
	
	
	$goods_type_list = $goodsType->getList($pageNow, $pageSize);
	
	$smarty->assign('pageNow', $pageNow);
	$smarty->assign('firstPage', 1);
	$smarty->assign('lastPage', $pageCount);
	$smarty->assign('pageCount', $pageCount);
	$smarty->assign('navigate', $navigate);
	$smarty->assign('goods_type_list', $goods_type_list);
	$smarty->assign('title', '商品类型');
	$smarty->assign('url', 'goods_type.php?act=add');
	$smarty->assign('link', '新建商品类型');
	
	$smarty->display('./admin/goods_type_list.html');
	
}else if($_GET['act'] == 'add'){
	$smarty->assign('action', 'insert');
	$smarty->assign('judge', 'add');
	$smarty->assign('title', '新建商品类型');
	$smarty->assign('url', 'goods_type.php?act=list');
	$smarty->assign('link', '商品类型列表');
	
	$smarty->display('./admin/goods_type_info.html');
	
}else if($_GET['act'] == 'insert'){
	
	$cat_name = trim($_POST['cat_name']);
	
	if($cat_name != ''){
		if($goodsType->cat_name_insert($cat_name)){
			$desc = '添加' . $cat_name . '成功';
			$url = 'goods_type.php?act=list';
			$link = '商品类型列表';
			admin_msg($smarty, $desc, $url, $link);
		}else{
			echo '添加失败';
		}
	}else{
		echo '商品名称不能为空!';
	}
	
}else if($_GET['act'] == 'delete'){
	
	$id = intval($_GET['id']);
	
	if($goodsType->delete($id)){
		echo 1;
	}else{
		echo '删除失败!';
	}
	
}else if($_GET['act'] == 'edit'){
	
	$cat_id = intval($_GET['cat_id']);
	
	if($row = $goodsType->getRow($cat_id)){
		$smarty->assign('row', $row);
		$smarty->assign('action', 'update');
		$smarty->assign('judge', 'edit');
		$smarty->assign('title', '编辑商品类型');
		$smarty->assign('url', 'goods_type.php?act=list');
		$smarty->assign('link', '商品类型列表');
		
		$smarty->display('./admin/goods_type_info.html');
	}else{
		echo '修改失败!';
	}
	
}else if($_GET['act'] == 'update'){
	
	$data = array();
	$data['cat_id'] = intval($_POST['cat_id']);
	$data['cat_name'] = trim($_POST['cat_name']);
	$data['enabled'] = intval($_POST['is_show']);
	
	if($goodsType->update($data)){
		$desc = '修改' . $data['cat_name'] . '成功';
		$url = 'goods_type.php?act=list';
		$link = '商品类型列表';
		admin_msg($smarty, $desc, $url, $link);
	}else{
		echo '修改商品失败!';
	}
	
}







?>